package com.xuecheng.learning.service.impl;

import com.alibaba.fastjson.JSON;
import com.xuecheng.learning.feignclient.ContentServiceClient;
import com.xuecheng.learning.feignclient.MediaServiceClient;
import com.xuecheng.learning.model.dto.XcCourseTablesDto;
import com.xuecheng.learning.model.po.XcCourseTables;
import com.xuecheng.learning.service.MyCourseTablesService;
import com.xuecheng.learning.service.MyLearningService;
import com.zmt.content.dto.TeachplanDto;
import com.zmt.content.po.CoursePublish;
import com.zmt.xuechengplusbase.model.RestResponse;
import com.zmt.xuechengplusbase.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class MyLearningServiceImpl implements MyLearningService {

    @Autowired
    private MyCourseTablesService myCourseTablesService;

    @Autowired
    private ContentServiceClient contentServiceClient;

    @Autowired
    private MediaServiceClient mediaServiceClient;


    /**
     * 对学习资格的校验以及获取学习视频地址返回
     *
     * @param userId      用户是否登录的凭证，传参可以传null因此不会报错
     * @param courseId
     * @param teachplanId
     * @param mediaId
     * @return
     */
    @Override
    public RestResponse<String> getVideo(String userId, Long courseId, Long teachplanId, String mediaId) {

        CoursePublish coursepublish = contentServiceClient.getCoursepublish(courseId);
        if (coursepublish == null) {
            return RestResponse.validfail("不存在该课程");
        }

        //如果支持试学的话，不用判断是否登录，直接进行看就ok
//        String teachplan = coursepublish.getTeachplan();
//        List<TeachplanDto> teachplanDtos = JSON.parseObject(teachplan, List.class);
        /**
         * 这通过课程id拿到了发布的课程基本信息，包括了课程计划（上面注释的两行）
         * 接下来是使用for循环和参数teachplanId进行对比（去公司里这么写不得把头拧下来啊……）
         */

        String teachPlanIsPreview = contentServiceClient.getTeachPlanIsPreview(teachplanId);
        if (teachPlanIsPreview.equals("1")){
            //说明可以试学,返回地址吧那就
            return mediaServiceClient.getPlayUrlByMediaId(mediaId);
        }

/*        for (int i = 0; i < teachplanDtos.size(); i++) {
            TeachplanDto teachplanDto = teachplanDtos.get(i);
            List<TeachplanDto> teachPlanTreeNodes = teachplanDto.getTeachPlanTreeNodes();
            for (int j = 0; j < teachPlanTreeNodes.size(); j++) {
                TeachplanDto teachplanDto1 = teachPlanTreeNodes.get(j);
                Long teachPlanId = teachplanDto1.getId();
                if (teachplanId.equals(teachPlanId)){
                    //说明用户要看这个视频，去判断能不能给他看 || 咋判断啊？
                }
            }
        }*/

        //首先对学习资格进行验证
        if (StringUtil.isNotEmpty(userId)) {
            //说明用户已经登录
            XcCourseTablesDto learningStatus = myCourseTablesService.getLearningStatus(userId, courseId);
            String learnStatus = learningStatus.getLearnStatus();
            if ("702002".equals(learnStatus)) {
                return RestResponse.validfail("没有选课或未支付");
            } else if ("702003".equals(learnStatus)) {
                return RestResponse.validfail("已过期需要申请续期或重新支付");
            } else {
                //说明有资格去观看视频
                //返回一个播放地址 调用媒资服务去获取文件的播放路径
                RestResponse<String> mediaUrl = mediaServiceClient.getPlayUrlByMediaId(mediaId);
                return mediaUrl;
            }
        }

        //说明还没有登录，判断课程是否可以进行免费学习
        String charge = coursepublish.getCharge();
        if ("201000".equals(charge)){
            //说明免费,返回播放地址去获取播放路径
            RestResponse<String> mediaUrl = mediaServiceClient.getPlayUrlByMediaId(mediaId);
            return mediaUrl;
        }
        return RestResponse.validfail("该课程需要付费");
    }


}
